package com.lingyue.commons.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * split list
 *
 * @author deep
 */
public class ListUtil<T> {

    public List<List<T>> splitList(List<T> list, int batchTotal) {
        int total = list.size();
        int count = (total % batchTotal == 0) ? (total / batchTotal) : ((total / batchTotal) + 1);
        List<List<T>> result = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            result.add(new ArrayList<>());
        }
        for (int i = 0; i < list.size(); i++) {
            result.get(i % count).add(list.get(i));
        }
        return result;
    }


    public List<List<T>> splitListTo(List<T> batchList, int count) {
        List<List<T>> batchSpanList = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            batchSpanList.add(new ArrayList<>());
        }
        for (int i = 0; i < batchList.size(); i++) {
            T current = batchList.get(i);
            batchSpanList.get(i % count).add(current);
        }
        return batchSpanList;
    }
}
